package com.simtop.car.service.imp;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.car.common.ServerResponse;

import com.simtop.car.mapper.CarMapper;
import com.simtop.car.mapper.SourceMapper;
import com.simtop.car.pojo.*;
import com.simtop.car.service.SourceService;
import com.simtop.car.utils.EmptyUtil;
import com.simtop.car.utils.RedisUtil;
import com.simtop.car.utils.WordFrequencyAnalyzer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SourceServiceImpl implements SourceService {

    @Autowired
    public SourceMapper sourceMapper;


    @Autowired
    public CarMapper carMapper;


    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Autowired
    public RedisUtil redisUtil;



    private Logger logger= LoggerFactory.getLogger(SourceService.class);

    @Override
    public List<Source> getSource() {
        return sourceMapper.getSource();
    }

    @Override
    public List<FieldMapping> getFieldMapping(Integer fieldSourceId) {
        return sourceMapper.getFieldMapping(fieldSourceId);
    }

    @Override
    public ServerResponse<PageInfo> getCarTypeSource(Integer pageNum, Integer pageSize, Integer sourceId,Date date,String[] brandType,User user,Date nowDate) {
        if(sourceId==1){
            PageHelper.startPage(pageNum,pageSize);
            List<WeiBo> weiBoList=sourceMapper.getWeiBo(date,nowDate,brandType);
            PageInfo weiBoPageInfo=new PageInfo<>(weiBoList);
            return ServerResponse.createBySuccess(weiBoPageInfo);
        }else if(sourceId==2){
            PageHelper.startPage(pageNum,pageSize);
            List<WeChat> weChatList=sourceMapper.getWeChat(date,nowDate,brandType);
            PageInfo<WeChat> weChatPageInfo=new PageInfo<>(weChatList);
            return ServerResponse.createBySuccess(weChatPageInfo);
        }else if(sourceId==3){
            PageHelper.startPage(pageNum,pageSize);
            List<News> newsList=sourceMapper.getNews(date,nowDate,brandType);
            PageInfo<News> newsPageInfo=new PageInfo<>(newsList);
            return ServerResponse.createBySuccess(newsPageInfo);
        }else if(sourceId==4){
            PageHelper.startPage(pageNum,pageSize);
            List<Recruitment> recruitmentList=sourceMapper.getRecruitment(date,nowDate,brandType);
            PageInfo<Recruitment> recruitmentPageInfo=new PageInfo<>(recruitmentList);
            return ServerResponse.createBySuccess(recruitmentPageInfo);
        }else if(sourceId==5){
            PageHelper.startPage(pageNum,pageSize);
            List<Patent> patentList=sourceMapper.getPatent(date,nowDate,brandType);
            PageInfo<Patent> patentPageInfo=new PageInfo<>(patentList);
            return ServerResponse.createBySuccess(patentPageInfo);
        }else if (sourceId==6){
            PageHelper.startPage(pageNum,pageSize);
            List<CompanyWebsite> companyWebsiteList=sourceMapper.getCompanyWebsite(date,nowDate,brandType);
            PageInfo<CompanyWebsite> companyWebsitePageInfo=new PageInfo<>(companyWebsiteList);
            return ServerResponse.createBySuccess(companyWebsitePageInfo);
        }else if(sourceId==7){
            PageHelper.startPage(pageNum,pageSize);
           List<CommunityForum> communityForumList=sourceMapper.getCommunityForum(date,nowDate,brandType);
            PageInfo<CommunityForum> communityForumPageInfo=new PageInfo<>(communityForumList);
           return ServerResponse.createBySuccess(communityForumPageInfo);
        }else if(sourceId==8){
            PageHelper.startPage(pageNum,pageSize);
            List<CorporateAnnualReports> corporateAnnualReportsList=sourceMapper.getCorporateAnnualReports(date,nowDate,brandType);
            PageInfo<CorporateAnnualReports> corporateAnnualReportsPageInfo=new PageInfo<>(corporateAnnualReportsList);
            return ServerResponse.createBySuccess(corporateAnnualReportsPageInfo);
        }else if(sourceId==9){
            PageHelper.startPage(pageNum,pageSize);
            List<BusinessInformation> businessInformationList=sourceMapper.getBusinessInformation(date,nowDate,brandType);
            PageInfo<BusinessInformation> businessInformationPageInfo=new PageInfo<>(businessInformationList);
            return ServerResponse.createBySuccess(businessInformationPageInfo);
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse<ConformityDataResult> dataIntegration(List<ConformityDataUtil> conformityDataUtilList,String[] brandType,User user)  {
        StringBuilder sb=new StringBuilder();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        List<ConformityData> conformityDataList=new ArrayList<>();
        conformityDataUtilList.forEach(conformityDataUtil -> {
            if (conformityDataUtil.getSourceId() == 1) {
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date = null;
                Date nowDate = null;
                try {
                    date = sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<WeiBo> weiBoList = sourceMapper.getWeiBoByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1, Arrays.toString(conformityDataUtil.getFieldMapping()).length() - 1), date, nowDate, brandType, user.getId());
                weiBoList.forEach(weiBo -> {
                    if (!ObjectUtils.isEmpty(weiBo)) {
                        ConformityData conformityData = new ConformityData();
                        conformityData.setSource("微博");
                        conformityData.setDate(weiBo.getPostedTime());
                        conformityData.setCompanyName(weiBo.getCarProduct());
                        conformityData.setArticle(weiBo.getBlogArticle());
                        conformityData.setUserId(user.getId());
                        conformityData.setCarProduct(weiBo.getCarProduct());
                        conformityDataList.add(conformityData);
                    }
                });
            } else if(conformityDataUtil.getSourceId()==2){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<WeChat> weChatList=sourceMapper.getWeChatByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                weChatList.forEach(weChat -> {
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("微信");
                    conformityData.setDate(weChat.getPostedTime());
                    conformityData.setCompanyName(weChat.getCarProduct());
                    conformityData.setTitle(weChat.getHeadline());
                    conformityData.setArticle(weChat.getMainBody());
                    conformityData.setUserId(user.getId());
                    conformityData.setCarProduct(weChat.getCarProduct());
                    conformityDataList.add(conformityData);
                });
            }else if(conformityDataUtil.getSourceId()==3){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<News> newsList=sourceMapper.getNewsByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                newsList.forEach(news -> {
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("新闻");
                    conformityData.setDate(news.getPostedTime());
                    conformityData.setCompanyName(news.getCarProduct());
                    conformityData.setTitle(news.getHeadline());
                    conformityData.setArticle(news.getMainBody());
                    conformityData.setUserId(user.getId());
                    conformityData.setCarProduct(news.getCarProduct());
                    conformityDataList.add(conformityData);
                });
            }else if(conformityDataUtil.getSourceId()==4){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<Recruitment> recruitmentList=sourceMapper.getRecruitmentByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                recruitmentList.forEach(recruitment -> {
                    logger.info("recruitment"+recruitment);
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("招聘");
                    conformityData.setDate(recruitment.getReleaseTime());
                    conformityData.setCompanyName(recruitment.getCompanyName());
                    conformityData.setTitle(recruitment.getJobTitle());
                    conformityData.setArticle(recruitment.getJobDescription());
                    conformityData.setUserId(user.getId());
                    conformityData.setCarProduct(recruitment.getCarProduct());
                    conformityDataList.add(conformityData);
                });
            }else if(conformityDataUtil.getSourceId()==5){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<Patent> patentList=sourceMapper.getPatentByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                patentList.forEach(patent -> {
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("专利");
                    conformityData.setDate(patent.getDateOfAuthorization());
                    conformityData.setCompanyName(patent.getPatentee());
                    conformityData.setTitle(patent.getName());
                    conformityData.setAbstracts(patent.getAbstracts());
                    conformityData.setArticle(patent.getDescription());
                    conformityData.setPatentClassificationNo(patent.getPatentClassificationNo());
                    conformityData.setUserId(user.getId());
                    conformityData.setCarProduct(patent.getCarProduct());
                    conformityDataList.add(conformityData);
                });
            }else if(conformityDataUtil.getSourceId()==6){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<CompanyWebsite> companyWebsiteList=sourceMapper.getCompanyWebsiteByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                companyWebsiteList.forEach(companyWebsite -> {
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("企业官网");
                    conformityData.setDate(companyWebsite.getTurnoverTime());
                    conformityData.setCompanyName(companyWebsite.getCompanyName());
                    conformityData.setTitle(companyWebsite.getProductName());
                    conformityData.setProductName(companyWebsite.getProductName());
                    conformityData.setProductDescription(companyWebsite.getProductDescription());
                    conformityData.setUserId(user.getId());
                    conformityData.setCarProduct(companyWebsite.getCarProduct());
                    conformityDataList.add(conformityData);
                });
            }else if(conformityDataUtil.getSourceId()==7){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<CommunityForum> communityForumList=sourceMapper.getCommunityForumByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                communityForumList.forEach(communityForum -> {
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("社区论坛");
                    conformityData.setDate(communityForum.getReleaseTime());
                    conformityData.setCompanyName(communityForum.getCarProduct());
                    conformityData.setArticle(communityForum.getPostDescription());
                    conformityData.setProductName(communityForum.getPostingTopic());
                    conformityData.setCarProduct(communityForum.getCarProduct());
                    conformityData.setUserId(user.getId());
                    conformityDataList.add(conformityData);
                });
            }else if(conformityDataUtil.getSourceId()==8){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<CorporateAnnualReports> corporateAnnualReportsList=sourceMapper.getCorporateAnnualReportsByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                corporateAnnualReportsList.forEach(corporateAnnualReports -> {
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("企业年报");
                    conformityData.setDate(corporateAnnualReports.getPostedTime());
                    conformityData.setCompanyName(corporateAnnualReports.getNameOfFirm());
                    conformityData.setAbstracts(corporateAnnualReports.getAnnualReport());
                    conformityData.setArticle(corporateAnnualReports.getAnnualReportInFull());
                    conformityData.setCarProduct(corporateAnnualReports.getCarProduct());
                    conformityData.setUserId(user.getId());
                    conformityDataList.add(conformityData);
                });
            }else if(conformityDataUtil.getSourceId()==9){
                for (String s : conformityDataUtil.getFieldMapping()) {
                    sb.append(s).append(",");
                }
                Date date=null;
                Date nowDate= null;
                try {
                    date=sdf.parse(conformityDataUtil.getDate()[0]);
                    nowDate = sdf.parse(conformityDataUtil.getDate()[1]);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<BusinessInformation> businessInformationList=sourceMapper.getBusinessInformationByIntegration(Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1),date,nowDate,brandType,user.getId());
                businessInformationList.forEach(businessInformation -> {
                    ConformityData conformityData=new ConformityData();
                    conformityData.setSource("工商信息");
                    conformityData.setDate(businessInformation.getDateOfEstablishment());
                    conformityData.setCompanyName(businessInformation.getNameOfFirm());
                    conformityData.setFirmWebsite(businessInformation.getFirmWebsite());
                    conformityData.setFirmAddress(businessInformation.getFirmAddress());
                    conformityData.setFirmIntroduction(businessInformation.getFirmIntroduction());
                    conformityData.setRegisteredCapital(businessInformation.getRegisteredCapital());
                    conformityData.setLegalRepresentative(businessInformation.getLegalRepresentative());
                    conformityData.setCarProduct(businessInformation.getCarProduct());
                    conformityData.setIndustryInvolved(businessInformation.getIndustryInvolved());
                    conformityData.setTypeOfEnterprise(businessInformation.getTypeOfEnterprise());
                    conformityData.setBusinessScope(businessInformation.getBusinessScope());
                    conformityData.setUserId(user.getId());
                    conformityDataList.add(conformityData);
                });
            }
        });
        String[] conformityMapping=sb.toString().split(",");
        List<ConformityMapping> conformityMappingList=new ArrayList<>();
        for (String s :conformityMapping) {
            ConformityMapping conformityMappingField=sourceMapper.getConformityMapping(s);
            if(!conformityMappingList.contains(conformityMappingField)&&EmptyUtil.isNotEmpty(conformityMappingField)){
                conformityMappingList.add(conformityMappingField);
            }
        }
        ConformityDataResult conformityDataResult=new ConformityDataResult();
        conformityDataResult.setConformityMappingList(conformityMappingList);
        logger.info("整合数据长度"+conformityDataList.size());
        if(redisUtil.hasKey("conformityData"+user.getId())){
            redisUtil.lUpdateIndex("conformityData"+user.getId(),0,conformityDataList);
        }else{
            redisUtil.lSet("conformityData"+user.getId(),conformityDataList,7200);
        }
        return ServerResponse.createBySuccess(conformityDataResult);
    }


    @Override
    public ServerResponse dataReleaseDate( String[] sourceId, String[] brandType) {
        Date earlyCheckDate=new Date();//最旧
        Date lateCheckDate=new Date();//最新
        Date date=new Date();//最旧
        Date lateDate=new Date();//最新
        for (int i = 0; i <sourceId.length; i++) {
            Integer sId=Integer.parseInt(sourceId[i]);
            if(sId==1){
                lateDate=sourceMapper.dataLateDateWeibo(brandType);
                date=sourceMapper.dataReleaseDateWeibo(brandType);
            }else if(sId==2){
                lateDate=sourceMapper.dataLateDateWeChat(brandType);
                date=sourceMapper.dataReleaseDateWeChat(brandType);
            }else if(sId==3){
                lateDate=sourceMapper.dataLateDateNews(brandType);
                date=sourceMapper.dataReleaseDateNews(brandType);
            }else if(sId==4){
                lateDate=sourceMapper.dataLateDateRecruitment(brandType);
                date=sourceMapper.dataReleaseDateRecruitment(brandType);
            }else if(sId==5){
                lateDate=sourceMapper.dataLateDatePatent(brandType);
                date=sourceMapper.dataReleaseDatePatent(brandType);
            }else if(sId==6){
                lateDate=sourceMapper.dataLateDateCompanyWebsite(brandType);
                date=sourceMapper.dataReleaseDateCompanyWebsite(brandType);
            }else if(sId==7){
                lateDate=sourceMapper.dataLateDateCommunityForum(brandType);
                date=sourceMapper.dataReleaseDateCommunityForum(brandType);
            }else if(sId==8){
                lateDate=sourceMapper.dataLateDateCorporateAnnualReports(brandType);
                date=sourceMapper.dataReleaseDateCorporateAnnualReports(brandType);
            }else if(sId==9){
                lateDate=sourceMapper.dataLateDateBusinessInformation(brandType);
                date=sourceMapper.dataReleaseDateBusinessInformation(brandType);
            }
            if(i==0){
                if(!StringUtils.isEmpty(date)&&!StringUtils.isEmpty(lateDate)){
                    earlyCheckDate=date;
                    lateCheckDate=lateDate;
                }
            }
            if(!StringUtils.isEmpty(date)&&!StringUtils.isEmpty(lateDate)){
                if(date.getTime()<earlyCheckDate.getTime()){
                    earlyCheckDate=date;
                }
                if(lateDate.getTime()>lateCheckDate.getTime()){
                    lateCheckDate=lateDate;
                }
            }
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
        String[] dates={sdf.format(earlyCheckDate),sdf.format(lateCheckDate)};
        return  ServerResponse.createBySuccess(dates);
    }


    @Override
    public List<BusinessInformation> getDisplayDataBusinessInformation(String[] brandType,Integer uId) {
        List<BusinessInformation> businessInformationList = sourceMapper.getDisplayDataBusinessInformation(brandType);
        return businessInformationList;
    }

    @Override
    public DisplayDataNews getDisplayDataNews(String[] brandType,Integer uId,Integer type) {
        DisplayDataNews displayDataNewsResult=new DisplayDataNews();
        List<String> sourceList=new ArrayList<>();
        List<String> yearList=new ArrayList<>();
        String uuid= (String) redisUtil.get("uuid"+uId);
        List<DisplayDataNews> displayDataNewsList=sourceMapper.getDisplayDataNews(brandType,uId,type,uuid);//根据
        List<DisplayDataNewsList> displayDataNewsLists=new ArrayList<>();
        displayDataNewsList.forEach(displayDataNews -> {
            if(!sourceList.contains(displayDataNews.getSource())){
                sourceList.add(displayDataNews.getSource());
            }
            if(!yearList.contains(displayDataNews.getYear())){
                yearList.add(displayDataNews.getYear());
            }
        });
        Collections.sort(yearList);
        String[] sourceArray=sourceList.toArray(new String[sourceList.size()]);
        String[] yearArray=yearList.toArray(new String[yearList.size()]);
        for (String source : sourceArray) {
            DisplayDataNewsList displayDataNewsList1=new DisplayDataNewsList();
            displayDataNewsList1.setName(source);
            List<DisplayDataNews> displayDataNewsList2=new ArrayList<>();
            displayDataNewsList.forEach(displayDataNews -> {
                if(source.equals(displayDataNews.getSource())){
                    displayDataNewsList2.add(displayDataNews);
                }
            });
            HashMap<String,Integer> hashMap=new HashMap<>();
            for (String year : yearArray) {
                hashMap.put(year,0);
            }
            StringBuilder sb=new StringBuilder();
            for (int i = 0; i <displayDataNewsList2.size(); i++) {
                if(hashMap.containsKey(displayDataNewsList2.get(i).getYear())){
                    hashMap.put(displayDataNewsList2.get(i).getYear(),displayDataNewsList2.get(i).getNums());
                }
            }
            for (String s : yearArray) {
                sb.append(hashMap.get(s)).append(",");
            }
            displayDataNewsList1.setData(sb.toString().split(","));
            displayDataNewsLists.add(displayDataNewsList1);
        }
        displayDataNewsResult.setLegend(sourceArray);
        displayDataNewsResult.setCategory(yearArray);
        displayDataNewsResult.setSeries(displayDataNewsLists);
        return displayDataNewsResult;
    }


    @Override
    public ServerResponse<DisplayDataNews> getDisplayDataPatent(String[] brandType, String data, Integer uId) {
        DisplayDataNews displayDataNewsResult=new DisplayDataNews();
        List<String> sourceList=new ArrayList<>();
        List<String> yearList=new ArrayList<>();
        String uuid= (String) redisUtil.get("uuid"+uId);
        List<DisplayDataNews> displayDataNewsList=sourceMapper.getDisplayDataPatent(brandType,data,uId,uuid);
        if(displayDataNewsList.size()==0){
            return ServerResponse.createByErrorMsg("暂无数据");
        }
        List<DisplayDataNewsList> displayDataNewsLists=new ArrayList<>();
        displayDataNewsList.forEach(displayDataNews -> {
            if(!sourceList.contains(displayDataNews.getSource())){
                sourceList.add(displayDataNews.getSource());
            }
            if(!yearList.contains(displayDataNews.getYear())){
                yearList.add(displayDataNews.getYear());
            }
        });
        Collections.sort(yearList);
        String[] sourceArray=sourceList.toArray(new String[sourceList.size()]);
        String[] yearArray=yearList.toArray(new String[yearList.size()]);
        for (String source : sourceArray) {
            DisplayDataNewsList displayDataNewsList1=new DisplayDataNewsList();
            displayDataNewsList1.setName(source);
            List<DisplayDataNews> displayDataNewsList2=new ArrayList<>();
            displayDataNewsList.forEach(displayDataNews -> {
                if(source.equals(displayDataNews.getSource())){
                    displayDataNewsList2.add(displayDataNews);
                }
            });
            HashMap<String,Integer> hashMap=new HashMap<>();
            for (String year : yearArray) {
                hashMap.put(year,0);
            }
            StringBuilder sb=new StringBuilder();
            for (int i = 0; i <displayDataNewsList2.size(); i++) {
                if(hashMap.containsKey(displayDataNewsList2.get(i).getYear())){
                    hashMap.put(displayDataNewsList2.get(i).getYear(),displayDataNewsList2.get(i).getNums());
                }
            }
            for (String s : yearArray) {
                sb.append(hashMap.get(s)).append(",");
            }
            displayDataNewsList1.setData(sb.toString().split(","));
            displayDataNewsLists.add(displayDataNewsList1);
        }
        displayDataNewsResult.setLegend(sourceArray);
        displayDataNewsResult.setCategory(yearArray);
        displayDataNewsResult.setSeries(displayDataNewsLists);
        return ServerResponse.createBySuccess(displayDataNewsResult);
    }


    @Override
    public ServerResponse<TotalData> getTotalData(String[] brandType,Integer uId) {
        Integer count=sourceMapper.getTotalDataCountById(uId);
        if(count==0){
            return  ServerResponse.createBySuccessMsg("暂无数据");
        }
        String[] selfBrandType={brandType[0]};
        String uuid= (String) redisUtil.get("uuid"+uId);
        Integer selfCount=sourceMapper.getTotalDataCount(selfBrandType,uId,uuid);
        if(brandType.length==1){
            TotalData totalData=new TotalData();
            totalData.setCount(count);
            totalData.setSelfCount(selfCount);
            totalData.setOtherCount(count-selfCount);
            return ServerResponse.createBySuccess(totalData);
        }
        String[] otherBrandType=new String[brandType.length-1];
        for (int i = 0; i <otherBrandType.length; i++) {
            otherBrandType[i]=brandType[i+1];
        }
        TotalData totalData=new TotalData();
        totalData.setCount(count);
        totalData.setSelfCount(selfCount);
        totalData.setOtherCount(count-selfCount);
        return ServerResponse.createBySuccess(totalData);
    }

    @Override
    public ServerResponse<List<PatentClassificationNoUtil>> getPatentClassificationNo(String[] brandType, Integer uId) {
        String uuid= (String) redisUtil.get("uuid"+uId);
        List<ConformityData> conformityDataList=sourceMapper.getPatentClassificationNo(brandType,uId,uuid);
        if(conformityDataList.size()==0){
            return ServerResponse.createByErrorMsg("暂无数据");
        }
        HashMap<String,Integer> hashMap=new HashMap<>();
        hashMap.put("A",0);
        hashMap.put("B",0);
        hashMap.put("C",0);
        hashMap.put("D",0);
        hashMap.put("E",0);
        hashMap.put("F",0);
        hashMap.put("G",0);
        hashMap.put("H",0);
        HashMap<String,String> hashMap1=new HashMap<>();
        hashMap1.put("A","人类生活必需");
        hashMap1.put("B","作业/运输");
        hashMap1.put("C","化学/冶金");
        hashMap1.put("D","纺织/造纸");
        hashMap1.put("E","固定建筑物");
        hashMap1.put("F","机械工程/照明/加热/武器/爆破");
        hashMap1.put("G","物理");
        hashMap1.put("H","电学");
        conformityDataList.forEach(conformityData -> {
            if(hashMap.containsKey(conformityData.getPatentClassificationNo().substring(0,1))){
                String key=conformityData.getPatentClassificationNo().substring(0,1);
                Integer value=hashMap.get(key);
                hashMap.put(key,value+1);
            }
        });
        List<PatentClassificationNoUtil> patentClassificationNoUtilList=new ArrayList<>();
        for (Map.Entry<String, Integer> stringIntegerEntry : hashMap.entrySet()) {
            PatentClassificationNoUtil patentClassificationNoUtil=new PatentClassificationNoUtil();
            patentClassificationNoUtil.setWord(stringIntegerEntry.getKey());
            patentClassificationNoUtil.setValue(stringIntegerEntry.getValue());
            patentClassificationNoUtil.setName(hashMap1.get(stringIntegerEntry.getKey()));
            patentClassificationNoUtilList.add(patentClassificationNoUtil);
        }
        return ServerResponse.createBySuccess(patentClassificationNoUtilList);
    }

    @Override
    public ServerResponse deleteSelfInfo(User user) {
//        String keys = "conformityData*";
//        Set keysSet = redisTemplate.keys(keys);
//        redisTemplate.delete(keysSet);
        sourceMapper.deleteExperiment(user.getId());//删除实验数据
        sourceMapper.deleteQuarter(user.getId());//删除季度数据
        return ServerResponse.createBySuccess();
    }

    @Override
    public List<ConformityData> filterConsolidatedData(String[] brandTypes,Integer uId,String source) {
        return sourceMapper.filterConsolidatedData(brandTypes,uId,source);
    }


    @Override
    public ServerResponse<List<FieldMappingResult>> getSourceField(Integer[] sourceId) {
        List<FieldMappingResult> fieldMappingResultList=new ArrayList<>();
        for (int i = 0; i < sourceId.length; i++) {
            FieldMappingResult fieldMappingResult=new FieldMappingResult();
            List<FieldMapping> fieldMappingList=sourceMapper.getSourceField(sourceId[i]);
            fieldMappingResult.setFieldMappingList(fieldMappingList);
            fieldMappingResult.setSourceId(sourceId[i]);
            fieldMappingResultList.add(fieldMappingResult);
        }
        return ServerResponse.createBySuccess(fieldMappingResultList);
    }


    @Override
    public ServerResponse<List> sourceWordFrequency(ConformityDataListResult conformityDataListResult) throws IOException {
        String[] brandType=conformityDataListResult.getBrandType().split(",");
        List<ConformityDataUtil> conformityDataUtilList=conformityDataListResult.getConformityDataUtilList();
        StringBuilder weiBoSB=new StringBuilder();
        StringBuilder weChatSB=new StringBuilder();
        StringBuilder newsSB=new StringBuilder();
        StringBuilder recruitmentSB=new StringBuilder();
        StringBuilder  patentSB=new StringBuilder();
        StringBuilder companySB=new StringBuilder();
        StringBuilder communitySB=new StringBuilder();
        StringBuilder corporateAnnualReportsSB=new StringBuilder();
        StringBuilder businessInformationSB=new StringBuilder();
        conformityDataUtilList.forEach(conformityDataUtil -> {
            Integer sourceId=conformityDataUtil.getSourceId();
            if(sourceId==1){
            List<String> list=sourceMapper.getWeiBoWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    weiBoSB.append(s).append(",");
                }
            }else if(sourceId==2){
                List<String> list=sourceMapper.getWeChatWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    weChatSB.append(s).append(",");;
                }
            }else if(sourceId==3){
                List<String> list=sourceMapper.getNewsWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    newsSB.append(s).append(",");
                }
            }else if(sourceId==4){
                List<String> list=sourceMapper.getRecruitmentWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    recruitmentSB.append(s).append(",");
                }
            }else if(sourceId==5){
                List<String> list=sourceMapper.getPatentWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    patentSB.append(s).append(",");
                }
            }else if(sourceId==6){
                List<String> list=sourceMapper.getCompanyWebsiteWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    companySB.append(s).append(",");
                }
            }else if(sourceId==7){
                List<String> list=sourceMapper.getCommunityForumWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    communitySB.append(s).append(",");
                }
            }else if(sourceId==8){
                List<String> list=sourceMapper.getCorporateAnnualReportsWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    corporateAnnualReportsSB.append(s).append(",");
                }
            }else if(sourceId==9){
                List<String> list=sourceMapper.getBusinessInformationWordFrequency(brandType,Arrays.toString(conformityDataUtil.getFieldMapping()).substring(1,Arrays.toString(conformityDataUtil.getFieldMapping()).length()-1));
                for (String s : list) {
                    businessInformationSB.append(s).append(",");
                }
            }
        });
        List<WordFrequencyPic> wordFrequencyPicList=new ArrayList<>();
        if(!StringUtils.isEmpty(weiBoSB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(weiBoSB.toString(), "微博");
            wordFrequencyPic.setSourceId(1);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(weChatSB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(weChatSB.toString(), "微信");
            wordFrequencyPic.setSourceId(2);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(newsSB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(newsSB.toString(), "新闻");
            wordFrequencyPic.setSourceId(3);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(recruitmentSB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(recruitmentSB.toString(), "招聘");
            wordFrequencyPic.setSourceId(4);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(patentSB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(patentSB.toString(), "专利");
            wordFrequencyPic.setSourceId(5);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(companySB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(companySB.toString(), "企业官网");
            wordFrequencyPic.setSourceId(6);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(communitySB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(communitySB.toString(), "社区论坛");
            wordFrequencyPic.setSourceId(7);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(corporateAnnualReportsSB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(corporateAnnualReportsSB.toString(), "企业年报");
            wordFrequencyPic.setSourceId(8);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        if(!StringUtils.isEmpty(businessInformationSB.toString())){
            WordFrequencyPic wordFrequencyPic = WordFrequencyAnalyzer.getWordFrequencyAnalyzer(businessInformationSB.toString(), "工商信息");
            wordFrequencyPic.setSourceId(9);
            wordFrequencyPicList.add(wordFrequencyPic);
        }
        return ServerResponse.createBySuccess(wordFrequencyPicList);
    }


    @Transactional
@Override
    public ServerResponse commitConformityData(User user) {
//    sourceMapper.deleteConformityData(user.getId());//删除之前整合数据的信息
        String uuid=UUID.randomUUID().toString();
        redisUtil.set("uuid"+user.getId(),uuid);
    List<ConformityData> conformityDataList = (List<ConformityData>) redisUtil.lGetIndex("conformityData" + user.getId(), 0);
    List<ConformityData> conformityDataList1 = new ArrayList<>();
    for (int i = 0; i < conformityDataList.size(); i++) {
        conformityDataList.get(i).setUuid(uuid);
        conformityDataList1.add(conformityDataList.get(i));
        if ((i + 1) % 500 == 0) {
            sourceMapper.insertConformityData(conformityDataList1);
            conformityDataList1.clear();
        }
    }
    if (conformityDataList1.size() >= 1) {
        sourceMapper.insertConformityData(conformityDataList1);
    }
    return ServerResponse.createBySuccess("存储成功");
}

    @Override
    public ServerResponse getAllSourceCount(SourceCount sourceCount) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//yyyy/MM/dd
        String date=sourceCount.getDate();
        String nowDate=sourceCount.getNowDate();
        Date dateResult=new Date();
        Date nowDateResult=new Date();
        try {
             dateResult=sdf.parse(date);
            nowDateResult=sdf.parse(nowDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer[] sourceId=sourceCount.getSourceId();
        int sum=0;
        for (int i = 0; i < sourceId.length; i++) {
            Integer sId=sourceId[i];
            if (sId==1){
                List<WeiBo> weiBoList=sourceMapper.getWeiBo(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=weiBoList.size();
            }else if(sId==2){
                List<WeChat> weChatList=sourceMapper.getWeChat(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=weChatList.size();
            }else if(sId==3){
                List<News> newsList=sourceMapper.getNews(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=newsList.size();
            }else if(sId==4){
                List<Recruitment> recruitmentList=sourceMapper.getRecruitment(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=recruitmentList.size();
            }else if(sId==5){
                List<Patent> patentList=sourceMapper.getPatent(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=patentList.size();
            }else if(sId==6){
                List<CompanyWebsite> companyWebsiteList=sourceMapper.getCompanyWebsite(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=companyWebsiteList.size();
            }else if(sId==7){
                List<CommunityForum> communityForumList=sourceMapper.getCommunityForum(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=communityForumList.size();
            }else if(sId==8){
                List<CorporateAnnualReports> corporateAnnualReportsList=sourceMapper.getCorporateAnnualReports(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=corporateAnnualReportsList.size();
            }else if(sId==9){
                List<BusinessInformation> businessInformationList=sourceMapper.getBusinessInformation(dateResult,nowDateResult,sourceCount.getBrandType().toString().split(","));
                sum+=businessInformationList.size();
            }
        }
        return ServerResponse.createBySuccess(sum);
    }
}
